package com.vvlive.caibao.smartcity.ui;

import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.vvlive.caibao.smartcity.adapter.MomentsRecyclerAdapter;
import com.vvlive.caibao.smartcity.base.BaseActivity;
import com.vvlive.caibao.smartcity.bean.Image;
import com.vvlive.caibao.smartcity.bean.Moment;
import com.vvlive.caibao.smartcity.data.BaseData;
import com.vvlive.caibao.smartcity.view.CommonActionBar;
import com.vvlive.caibao.smartcity.view.CommonActionBar.OnMoreListener;

import org.kymjs.kjframe.ui.BindView;
import org.kymjs.kjframe.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

import smartcity.vvlive.caibao.com.smartc.R;

/**
 * Created by zhj on 2015/7/2.
 */
public class MomentsActivity extends BaseActivity implements OnMoreListener, OnRefreshListener {

    @BindView(id = R.id.moments_list_container)
    SwipeRefreshLayout refreshLayout;

    @BindView(id = R.id.moments_recycler)
    RecyclerView momentsRecycler;

    private String[][] images = new String[][]{
           {"http://f.hiphotos.baidu.com/image/w%3D400/sign=2fcfc9090ed79123e0e095749d345917/ae51f3deb48f8c5471a15c2e38292df5e0fe7f45.jpg", "1280", "800"}
           , {"http://img3.douban.com/view/photo/photo/public/p2249526036.jpg", "640", "960"}
           , {"http://h.hiphotos.baidu.com/image/w%3D310/sign=56b9a5d28813632715edc432a18ea056/d52a2834349b033b1cd3c5d417ce36d3d539bd60.jpg", "250", "250"}
           , {"http://h.hiphotos.baidu.com/image/w%3D310/sign=bda7e807372ac65c67056072cbf2b21d/8435e5dde71190eff1e6a6b8cc1b9d16fdfa60b3.jpg", "250", "250"}
           , {"http://a.hiphotos.baidu.com/image/w%3D310/sign=8595fac69b25bc312b5d07996ede8de7/9e3df8dcd100baa1657168b34510b912c8fc2e5c.jpg", "250", "250"}
           , {"http://c.hiphotos.baidu.com/image/w%3D310/sign=f072b918cb95d143da76e22243f18296/b21c8701a18b87d695aafe04040828381e30fdc5.jpg", "250", "250"}
           , {"http://g.hiphotos.baidu.com/image/w%3D310/sign=5803fd085143fbf2c52ca022807eca1e/91ef76c6a7efce1b034910caad51f3deb48f6554.jpg", "250", "250"}
           , {"http://g.hiphotos.baidu.com/image/w%3D310/sign=5698a672af4bd11304cdb1336aafa488/a08b87d6277f9e2f99055f091d30e924b899f3fb.jpg", "250", "250"}
           , {"http://img4.douban.com/view/photo/photo/public/p2252689992.jpg", "1280", "800"}
    };

    @Override
    protected int getContentViewRes() {
        return R.layout.activity_moments;
    }

    private List<Moment> moments = new ArrayList<>();
    private LinearLayoutManager mLinearLayoutManager;
    private MomentsRecyclerAdapter mAdapter;


    @Override
    public void initWidget() {
        super.initWidget();

        final CommonActionBar actionBar = getCommonActionBar();
        actionBar.setTitle("朋友圈");
        actionBar.setMoreIcon(R.mipmap.xiangji);
        actionBar.setOnMoreListener(this);
        actionBar.setBackgroundColor(Color.TRANSPARENT);
        setCommonActionBarFloating(true);

        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setColorSchemeColors(Color.parseColor("#FE6661"));

        // 初始化朋友圈的数据
        moments.addAll(initMomentsData());

        mLinearLayoutManager = new LinearLayoutManager(this);
        momentsRecycler.setLayoutManager(mLinearLayoutManager);
        momentsRecycler.setItemAnimator(new DefaultItemAnimator());
        momentsRecycler.setHasFixedSize(true);
        mAdapter = new MomentsRecyclerAdapter(moments, this);
        momentsRecycler.setAdapter(mAdapter);

        momentsRecycler.addOnScrollListener(new RecyclerView.OnScrollListener() {

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                // 得到滚动的距离，作为ActionBar的透明度
                int alpha = recyclerView.computeVerticalScrollOffset();

                if (alpha <= 255) {
                    actionBar.setBackgroundAlpha(alpha);
                } else {
                    actionBar.setBackgroundAlpha(255);
                }

                //
                int lastPosition = mLinearLayoutManager.findLastVisibleItemPosition();
                boolean whetherLoadMore = lastPosition == moments.size() - 1;
                if(whetherLoadMore){

                    moments.addAll(initMomentsData());
                    mAdapter.notifyDataSetChanged();
                }
            }
        });

    }

    /**
     * 初始化朋友圈模拟数据
     */
    private List<Moment> initMomentsData() {
        List<Moment> momentsTemp = new ArrayList<>();
        //这里单独添加一条单条的测试数据，用来测试单张的时候横竖图片的效果
        ArrayList<Image> singleList = new ArrayList<Image>();
        singleList.add(new Image(images[8][0], Integer.parseInt(images[8][1]), Integer.parseInt(images[8][2])));
        Moment moment = null;
        //从一到9生成9条朋友圈内容，分别是1~9张图片
        for (int i = 0; i < 9; i++) {

            moment = new Moment();
            moment.setUserPortrait(BaseData.urls[i]);
            moment.setPublisherName(BaseData.names[i]);
            moment.setContent("One piece of moment in CardView.");

            ArrayList<Image> itemList = new ArrayList<>();
            for (int j = 0; j <= i; j++) {
                itemList.add(new Image(images[j][0], Integer.parseInt(images[j][1]), Integer.parseInt(images[j][2])));
            }
            moment.setImages(itemList);
            moment.setPublishTime("Created at " + StringUtils.friendlyTime("2015-07-29 10:19:00"));

            momentsTemp.add(moment);
        }
        return momentsTemp;
    }

    @Override
    public void onMore(View v) {

        // 点击右上角相机按钮，跳转到发表朋友圈页面
        newActivity(PublishOneMomentActivity.class);
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    refreshLayout.setRefreshing(false);
                    moments = initMomentsData();
                    mAdapter.notifyDataSetChanged();
                    toast("刷新成功");
                    break;
            }
        }
    };

    @Override
    public void onRefresh() {
        handler.sendEmptyMessageDelayed(1, 2000);
    }
}
